<template>
<div :class="$style.container">
  <group :gutter="10">
    <cell>
      <div slot="title" :class="$style.title">猜你喜欢</div>
    </cell>
    <ul>
      <li v-for="(item, idx) in lists.slice(0,6)" :key="'film'+idx" @click.stop="handleItemClick(item)">
        <div :class="$style.left">
          <img v-lazy="item.poster"/>
          <div :class="$style.content">
            <h4>{{item.original_title}}</h4>
            <div :class="$style.see"><em>{{item.wish_count}}</em> 人想看</div>
            <div :class="$style.actor">{{item.actors.map(item => item.name).join('、')}}</div>
            <div :class="$style.rate">观众评分 <em>{{item.rate}}</em></div>
          </div>
        </div>
        <div :class="$style.right">
          <span :class="$style.btn" v-if="item.price && !item.isMyFilm">购买</span>
          <span :class="$style.btn" v-else-if="item.price && item.isMyFilm" style="background: #3c9fe6;">已购买</span>
          <span :class="$style.free" v-else>免费</span>        
        </div>
      </li>
    </ul>
  </group>
</div>
</template>

<script>
import { Group, Cell, XButton } from 'vux'
import http from '../../../APIService/http'
export default {
  props: {
    lists: {
      type: Array,
      default: () => []
    }
  },
  components: {
    Group,
    Cell,
    XButton
  },
  methods: {
    // 点击
    handleItemClick (item) {
      if (item.price && !item.isMyFilm) {
        this.handleBuyClick(item)
      } else {
        this.$router.push({
          path: '/detail',
          query: { id: item.doubanId }
        })
      }
    },
    /**
     * 立即购买
     */
    async handleBuyClick (item) {
      try {
        let token = !this.$Cookie.getToken()
        if (!token) {
          this.$router.push({
            path: '/login'
          })
          return false
        }
        let params = {
          id: item.doubanId
        }
        let res = await http.post(`/cart/addCart`, params)
        if (res.code === 0) {
          this.$router.push({
            path: '/cart'
          })
        }
      } catch (err) {
        throw err
      }
    }
  }
}
</script>

<style lang='less' module>
.container {
  .title {
    font-size: 14px;
    color: #333;
    font-weight: bold;
  }
  ul {
    padding: 10px;
    box-sizing: border-box;
    li {
      padding-bottom: 10px;
      display: flex;
      flex-direction: row;
      flex-wrap: nowrap;
      align-items: center;
      justify-content: space-between;
      .left {
        img{
          width: 64px;
          height:90px;
          vertical-align: top;
          object-fit: cover;
        }
        .content {
          display: inline-block;
          width: 220px;
          h4 {
            font-size: 17px;
            color: #333;
            font-weight: 700;
            padding-right: 5px;
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            line-height:24px;
          }
          .see {
            font-size: 13px;
            color: #666;
            line-height:20px;
            em {
              color: #faaf00;
              font-size: 15px;
              font-weight: 600;
            }
          }
          .actor{
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            font-size: 13px;
            color: #666;
            line-height: 20px;
          }
          .rate {
            font-size: 13px;
            color: #666;
            line-height:20px;
            em {
              font-weight: 700;
              color: #faaf00;
              font-size: 15px;
            }
          }
        }
      }
      .right {
        width: 60px;
        .btn {
          display: inline-block;
          height:27px;
          width: 47px;
          line-height: 28px;
          text-align: center;
          box-sizing: border-box;
          background-color: #f03d37;
          color: #fff;
          border-radius: 4px;
          white-space: nowrap;
          font-size: 12px;
          cursor: pointer;
        }
        .free {
          color:#be9e4d;
        }
      }
    }
  }
}

</style>